package cn.com.api.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiAddress;
import cn.com.api.service.ApiAddressService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "/address")
public class ApiAddressController {

	@Autowired
	private ApiAddressService addressService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiAddressController.class);

	// 查询用户所有的地址
	@RequestMapping("/getAddresslist")
	public void getAddresslist(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String u_id = request.getParameter("u_id");
		// String token = request.getParameter("token");
		List<ApiAddress> list = new ArrayList<ApiAddress>();
		try {
			System.out.println(u_id);
			// if (checkService.checkToken(u_id, token, map)) {
			list = addressService.getAddressList(u_id);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put("datas", list);
			// }
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	// 查询用户使用的地址
	@RequestMapping("/searchEnable_address")
	public void getAddressEnable(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String uid = request.getParameter("uid");
		String token = request.getParameter("token");
		ApiAddress list = new ApiAddress();
		try {
			if (checkService.checkToken(uid, token, map)) {
				list = addressService.getAddressEnable(uid);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put("datas", list);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 根据ID查询用户的地址
	@RequestMapping("/searchaddressById")
	public void getAddressById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		String u_id = request.getParameter("u_id");
		String token = request.getParameter("token");
		ApiAddress list = new ApiAddress();
		try {
			if (checkService.checkToken(u_id, token, map)) {
				list = addressService.getAddressById(id);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put("datas", list);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 修改收货地址
	@RequestMapping("/update_address")
	public void update(ApiAddress addres, HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Integer enable = addres.getEnable();
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			if (checkService.checkToken(u_id, token, map)) {
				if (checkService.checkAddress(addres, map)) {
					if (enable == 1) {
						addressService.updateAllEnable(u_id);
					}
					addressService.update(addres);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "修改成功");
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(JsonUtil.object2json(map));
		response.getWriter().close();
	}

	// 停用所有地址、启用选中地址
	@RequestMapping("/updateSate_address")
	public void updateState(ApiAddress address, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		String uid = request.getParameter("uid");
		String token = request.getParameter("token");
		try {
			if (checkService.checkToken(uid, token, map)) {
				address.setU_id(uid);
				addressService.updateAllEnable(address.getU_id());
				addressService.updateSignStart(address.getId());
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(JsonUtil.object2json(map));
		response.getWriter().close();
	}

	// 删除地址
	@RequestMapping("/delteaddress")
	public void updateState(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();

		String id = request.getParameter("id");
		String u_id = request.getParameter("u_id");
		String token = request.getParameter("token");
		try {
			if (checkService.checkToken(u_id, token, map)) {
				addressService.deleteAddr(Integer.valueOf(id));
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "删除成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
}
